package com.fairytask.engine.components.navigation
{
	import com.fairytask.engine.components.BaseComponent;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.group.ViewGroup;
	import com.fairytask.engine.components.text.Input;
	import com.fairytask.engine.resource.StaticIni;
	
	import flash.events.MouseEvent;

	/**
	 * 分页显示 
	 * @author zhuy
	 * 
	 */	
	public class Navigation extends BaseComponent
	{	
		/**当前页数*/
		private var _curPageNum	:	int = 1;
		/**总页数*/
		private var _totalPageNum	:	int = 1;
		/**首页按钮*/
		private var _firtBtn		:	Button;
		/**尾页按钮*/
		private var _endBtn		:   Button;
		/**上一页按钮*/
		private var _lastBtn		:	Button;
		/**下一页按钮*/
		private var _nextBtn		:	Button;
		/**当前页数/总页数 输入框*/
		private var _pageInput		:	Input;
		/**按钮资源*/
		private var _btnRes		:	String;
		/**按钮固定宽度值*/
		private var btnWidth		:   int=60;
		/**按钮固定高度值*/
		private var btnHeight		:   int=24;
		/**输入框固定宽度*/
		private var inputWidth		:   int=55;
		/**输入框固定高度*/
		private var inputHight		:   int=20;
		/**每页固定最多显示项数*/
		private var _perPageNum	:   uint=0;
		/**总项数*/
		private var _totalItemNum	:   uint=0;
		/**分页类型*/
		private var _type			:   int=1;
		/**分页页数显示是否初始化*/
		private var _initPageShow	:   Boolean;   
	
		
		/**
		 * 
		 * @param perPageNum	每页固定最多显示项数
		 * @param totalItemNum 	总项数
		 * @param btnRes		按钮资源
		 * @param type			分页类型(1.不含“首页”、“尾页”按钮;2.含俩按钮)
		 * 
		 */	
		public function Navigation(perPageNum:uint=0,totalItemNum:uint=0,type:uint=1,btnRes:String="Button_0103",_btnWidth:uint=0,_btnHeight:uint=0,_inputWidth:uint=0,_inputHight:uint=0)
		{
			super();
			_btnRes			= btnRes;
			if(_btnWidth!=0 && _btnHeight!=0)
			{
				btnWidth = _btnWidth;
				btnHeight= _btnHeight;
			}
			if(_inputWidth!=0 && _inputHight!=0)
			{
				inputWidth = _inputHight;
				inputHight = _inputHight;
			}
			_pageInput 		= new Input(inputWidth,inputHight,"textSkin_1");
			_lastBtn 		= new Button(StaticIni.getSwfLangStr("1000000375"),btnWidth,btnHeight,_btnRes);
			_nextBtn 		= new Button(StaticIni.getSwfLangStr("1000000376"),btnWidth,btnHeight,_btnRes);
			_nextBtn.x 		= 188;
			_lastBtn.enabled= false;
			_nextBtn.enabled= false;
			_pageInput.x	= 95;
			_pageInput.y	= 1;
			_pageInput.editable = false;
			_pageInput.width= 43;
			_type			= type;
			
			if(_type!=1)
			{
				_firtBtn 		= new Button(StaticIni.getSwfLangStr("1000000377"),btnWidth,btnHeight,_btnRes);
				_endBtn  		= new Button(StaticIni.getSwfLangStr("1000000378"),btnWidth,btnHeight,_btnRes);
				_firtBtn.enabled= false;
				_endBtn.enabled = false;
				//位置按效果图配
				_lastBtn.x 		= 65;
				_pageInput.x 	= 133;
				_endBtn.x		= 250;
				this.addChild(_firtBtn);
				this.addChild(_endBtn);
				
			}
		
			if(perPageNum!=0&&totalItemNum!=0)
			{
				_initPageShow 	= true; 
				_lastBtn.enabled= true;
				_nextBtn.enabled= true;
				if(_firtBtn)
				{
					_firtBtn.enabled = true;
				}
				if(_endBtn)
				{
					_endBtn.enabled = true;
				}
				//_perPageNum、totalItemNum被传入值，点击事件可用
				initEvent();
				_perPageNum 	= perPageNum;
				_totalItemNum	= totalItemNum;
				if((totalItemNum%perPageNum)!=0)
				{
					_totalPageNum 	= totalItemNum/perPageNum + 1;
				}else
				{
					_totalPageNum   = totalItemNum/perPageNum;
				}
				pageInputShow();
				btnFilter();
			}
			
			this.addChild(_lastBtn);
			this.addChild(_nextBtn);
			this.addChild(_pageInput);
		}

		/**
		 * 初始化显示 "当前页/总页数"(供创建类时未初始化“每页固定最多显示项数”、“总项数”，或者分页控制更改) 
		 * @param curPageNum 
		 * @param totalPageNum
		 * 
		 */		
		public function  setNavigationShowInit(perPageNum:uint=0,totalItemNum:uint=0):void
		{
			//未初始化分页显示页数数据
			if(!_initPageShow)
			{
				_lastBtn.enabled = true;
				_nextBtn.enabled = true;
				if(_type==2)
				{
					_firtBtn.enabled = true;	
					_endBtn.enabled = true;
				}
				initEvent();
			}
		
			if(perPageNum!=0&&totalItemNum!=0)
			{
				_perPageNum 	=	perPageNum;
				_totalItemNum	= 	totalItemNum;
				if((totalItemNum%perPageNum)!=0)
				{
					_totalPageNum 	= totalItemNum/perPageNum + 1;
				}else
				{
					_totalPageNum   = totalItemNum/perPageNum;
				}
			}else
			{
				_pageInput.htmlStr = " ";
				_lastBtn.enabled = false;
				_nextBtn.enabled = false;
				if(_type==2)
				{
					_firtBtn.enabled = false;	
					_endBtn.enabled  = false;
				}
				return;
			}
			
			_curPageNum     =   1;
			pageInputShow();
			btnFilter();
		}
		
		/**
		 * 设置输入框与按钮的水平间距 
		 * 
		 */		
		public function setHorSpace(hspace:int):void
		{
			if(_type==1)
			{
				_pageInput.x	= _lastBtn.width + hspace;
				_nextBtn.x 		= _pageInput.x + _pageInput.width + hspace;
			}else
			{
				_pageInput.x 	= _lastBtn.x + _lastBtn.width + hspace;
				_nextBtn.x 		= _pageInput.x + _pageInput.width + hspace;
				_endBtn.x		= _nextBtn.x + _nextBtn.width + 6;
			}
		}
		
		/**
		 * 设置当前页数 
		 * @param curPageNum
		 * @return 
		 * 
		 */		
		public function  setCurPageShow(curPageNum:int):void
		{
			if(curPageNum == 0)
			{
				clearFilter();
				return;
			}
			if(curPageNum>_totalPageNum)
			{
				return;
			}
			_curPageNum = curPageNum;
			pageInputShow();
			btnFilter();
		}
		
		/**
		 * 数据清空过滤 
		 * 
		 */		
		private function clearFilter():void
		{
			if(_type==1)
			{
				_lastBtn.enabled = false;
				_nextBtn.enabled = false;
			}
			else
			{
				_lastBtn.enabled = false;
				_nextBtn.enabled = false;
				_firtBtn.enabled = false;
				_endBtn.enabled  = false;
			}
			_pageInput.htmlStr = "";
		}
		
		/**
		 * 根据当前页与总页数的关系，是否屏蔽处理 
		 * 
		 */		
		private function btnFilter():void
		{
			if(_totalPageNum==1)
			{
				if(_type==1)
				{
					_lastBtn.enabled = false;
					_nextBtn.enabled = false;
				}
				else
				{
					_lastBtn.enabled = false;
					_nextBtn.enabled = false;
					_firtBtn.enabled = false;
					_endBtn.enabled  = false;
				}
			}
			else
			{
				if((_curPageNum>1)&&(_curPageNum<_totalPageNum))
				{
					if(_type==1)
					{
						_lastBtn.enabled = true;
						_nextBtn.enabled = true;
					}
					else
					{
						_lastBtn.enabled = true;
						_nextBtn.enabled = true;
						_firtBtn.enabled = true;
						_endBtn.enabled  = true;
					}
				}
				if(_curPageNum==1)
				{
					if(_type==1)
					{
						_lastBtn.enabled = false;
						_nextBtn.enabled = true;
					}
					else
					{
						_lastBtn.enabled = false;
						_firtBtn.enabled = false;
						_nextBtn.enabled = true;
						_endBtn.enabled  = true;
					}
				}
				if(_curPageNum==_totalPageNum)
				{
					if(_type==1)
					{
						_nextBtn.enabled = false;
						_lastBtn.enabled = true;
					}
					else
					{
						_nextBtn.enabled = false;
						_endBtn.enabled  = false;
						_lastBtn.enabled = true;
						_firtBtn.enabled = true;
					}
				}
			}
		}
		
		/**
		 * 初始化按钮点击事件事件 
		 * 
		 */		
		private function initEvent():void
		{
			_lastBtn.addEventListener(MouseEvent.CLICK,onLast);
			_nextBtn.addEventListener(MouseEvent.CLICK,onNext);
			if(_type!=1)
			{
				_firtBtn.addEventListener(MouseEvent.CLICK,onFirt);
				_endBtn.addEventListener(MouseEvent.CLICK,onEnd);
			}
		}

		
		/**
		 * 输入框显示 当前页数/总页数 
		 * @param curPageNum
		 * @param totalPageNum
		 * 
		 */		
		private function pageInputShow():void
		{
			_pageInput.htmlStr = _curPageNum.toString() + "/" + _totalPageNum.toString();
		}
		
		/**
		 * 点击上一页按钮 
		 * @param evt
		 * 
		 */		
		private function onLast(evt:MouseEvent):void
		{
			_curPageNum--;
			if(!_nextBtn.enabled)
			{
				_nextBtn.enabled = true;
			}
			if(_endBtn&&!_endBtn.enabled)
			{
				_endBtn.enabled = true;
			}
			if(_curPageNum==1)
			{
				_lastBtn.enabled = false;
				if(_firtBtn)
				{
					_firtBtn.enabled = false;
				}
			}
			var indexData:Array = getItemIndex();
			var startIndex:uint = indexData[0];
			var endIndex:uint   = indexData[1];
			this.dispatchEvent(new NavigationEvent(NavigationEvent.NAVIGATION_EVENT,[_curPageNum,startIndex,endIndex]));
			pageInputShow();
		}
		
		/**
		 * 当前页的包含 “开始项索引”、“结束项索引”的数组 
		 * @return 
		 * 
		 */		
		public function getItemIndex():Array
		{
			var startIndex:uint = (_curPageNum-1)*_perPageNum;
			var endIndex  :uint;
			if(_curPageNum<_totalPageNum)
			{
				endIndex = _curPageNum*_perPageNum -1;
			}else
			{
				endIndex = _totalItemNum - 1;
			}
			return [startIndex,endIndex];
		}
		
		/**
		 * 点击下一页按钮 
		 * @param evt
		 * 
		 */		
		private function onNext(evt:MouseEvent):void
		{
			_curPageNum++;
			if(!_lastBtn.enabled)
			{
				_lastBtn.enabled = true;
			}
			if(_firtBtn&&!_firtBtn.enabled)
			{
				_firtBtn.enabled = true;
			}
			if(_curPageNum==_totalPageNum)
			{
				_nextBtn.enabled = false;
				if(_endBtn)
				{
					_endBtn.enabled = false;
				}
			}
			var indexData:Array = getItemIndex();
			var startIndex:uint = indexData[0];
			var endIndex:uint   = indexData[1];
			this.dispatchEvent(new NavigationEvent(NavigationEvent.NAVIGATION_EVENT,[_curPageNum,startIndex,endIndex]));
			pageInputShow();
		}
		
		/**
		 * 点击首页按钮 
		 * @param evt
		 * 
		 */		
		private function onFirt(evt:MouseEvent):void
		{
			_curPageNum = 1;
			_firtBtn.enabled	= false;
			_lastBtn.enabled 	= false;
			if(_totalPageNum>1)
			{
				_nextBtn.enabled= true;
				if(!_endBtn.enabled)
				{
					_endBtn.enabled = true;
				}
			}
			var indexData:Array = getItemIndex();
			var startIndex:uint = indexData[0];
			var endIndex:uint   = indexData[1];
			this.dispatchEvent(new NavigationEvent(NavigationEvent.NAVIGATION_EVENT,[_curPageNum,startIndex,endIndex]));
			pageInputShow();
		}
		
		/**
		 * 点击尾页按钮 
		 * @param evt
		 * 
		 */		
		private function onEnd(evt:MouseEvent):void
		{
			_curPageNum = _totalPageNum;
			_nextBtn.enabled	= false;
			_endBtn.enabled     = false;
			if(_totalPageNum>1)
			{
				_lastBtn.enabled= true;
				if(!_firtBtn.enabled)
				{
					_firtBtn.enabled = true;
				}
			}
			var indexData:Array = getItemIndex();
			var startIndex:uint = indexData[0];
			var endIndex:uint   = indexData[1];
			this.dispatchEvent(new NavigationEvent(NavigationEvent.NAVIGATION_EVENT,[_curPageNum,startIndex,endIndex]));
			pageInputShow();
		}
		
		public function totalPage(_tPage:int):void
		{
			_totalPageNum	=	_tPage;
		}
		
		public function get curPageNum():int
		{
			return _curPageNum;
		}
		
		public function get totalPageNum():int
		{
			return _totalPageNum;
		}
	}
}